<?php
class Model_Game extends Zend_Db_Table_Abstract{
	protected $_name="game";
	protected $_primary="game_id";
	protected   $db;
	public function __construct(){
		parent::__construct();
		$this->db = Zend_Registry::get('db');
	}
	public function listall(){
		$sql=$this->db->query("select * from game");
		return $sql->fetchAll();
	}
	public function getGameById($gameId) {
		$select = $this->db->select();
		$select->from("game");
		$select->joinLeft("game_category", "game.category = game_category.id");
		$select->where("game_id =?",$gameId);
		return $select->query()->fetchAll();
	}
	public function getGameImageById($gameId) {
		$select = $this->db->select();
		$select->from('game_image_detail');
		$select->where("game_id =?",$gameId);
		return $select->query()->fetchAll();
	}
	public function getNewGame(){
		$select = $this->select();
		$select->order("update_time DESC");
		return $this->fetchAll($select)->toArray();
	}
	public function getHotGame(){
		$select = $this->db->select();
		$select->from('game');
		$select->joinLeft("user_download_history", "game.game_id = user_download_history.game_id and user_download_history.create_time >= NOW() - INTERVAL 7 DAY")
				->columns(array("d" => "count(user_download_history.id)"))
				->group("game.game_id")
				->order("d DESC");
		//return $select->__toString();
		return $select->query()->fetchAll();
	}
	public function addLike($gameId){
		$sql=$this->db->query("update game set game.like = game.like+1 where game_id = ".$gameId);
	}
	public function addShare($gameId){
		$sql=$this->db->query("update game set game.share = game.share+1 where game_id = ".$gameId);
	}
	public function addDownload($gameId){
		$sql=$this->db->query("insert into user_download_history(game_id, create_time) values (".$gameId.",NOW())");
		$sql=$this->db->query("update game set game.download = game.download+1 where game_id = ".$gameId);
	}
}
?>